package legumes.client.ui.basket.grids;


import java.util.ArrayList;

import legumes.client.evt.main.UserConnectedEvent;
import legumes.client.evt.main.UserConnectedEventHandler;
import legumes.client.evt.manager.EventBusManager;
import legumes.shared.domain.MenuCount;
import legumes.shared.domain.MenuItem;
import legumes.shared.domain.Order;
import legumes.shared.domain.OrderPart;
import legumes.shared.domain.OrderPartMenuItem;
import legumes.shared.domain.OrderPartProductItem;
import legumes.shared.domain.SellSessionForCustomer;

import com.google.gwt.core.client.Duration;
import com.smartgwt.client.widgets.layout.VLayout;

public class MainCenterGridsPanel extends VLayout {

	private BasketInsidePanel basketInsidePanel;
	private BasketOutsidePanel basketOutsidePanel;
	
	public MainCenterGridsPanel() {
		super();
		setWidth("700");
		setHeight100();
        
		setMembersMargin(5);  
        setLayoutMargin(10); 
        
        basketInsidePanel = new BasketInsidePanel();
        basketOutsidePanel = new BasketOutsidePanel();
        
        setMembers(basketInsidePanel, basketOutsidePanel);
	}
	
	public void initData(final SellSessionForCustomer sellSessionForCustomer_){
		OrderPart op = initOrderData(sellSessionForCustomer_);
    	
		EventBusManager.getInstance().getEventBus().addHandler(
				UserConnectedEvent.TYPE, 
				new UserConnectedEventHandler() {
					
					@Override
					public void onUserConnected(UserConnectedEvent event) {
						if( sellSessionForCustomer_.getOrder().getCustomerId() == null
								|| sellSessionForCustomer_.getOrder().getCustomerName() == null){
							sellSessionForCustomer_.getOrder().setCustomerId(event.getCustomerDTO().getEmail());
							sellSessionForCustomer_.getOrder().setCustomerId(event.getCustomerDTO().getName());
						}
						
					}
				}
		);
		
		basketInsidePanel.initData(sellSessionForCustomer_, op);
		basketOutsidePanel.initData(sellSessionForCustomer_, op);
	}
	
	private OrderPart initOrderData(SellSessionForCustomer sellSessionForCustomer_){
		long creationDate = (long)Duration.currentTimeMillis();
		if( sellSessionForCustomer_.getOrder() == null){
			Order o = new Order();
			o.setId(1);
			o.setDate(creationDate);
			sellSessionForCustomer_.setOrder(o);
		}
		
		if( sellSessionForCustomer_.getOrder().getOrderParts() == null ){
			sellSessionForCustomer_.getOrder().setOrderParts(new ArrayList<OrderPart>(1));
		}
		OrderPart op = createorderPart(sellSessionForCustomer_, creationDate);
		sellSessionForCustomer_.getOrder().getOrderParts().add( op );
//		if( sellSessionForCustomer_.getOrder().getOrderParts().size()<=0){
//			
//		} else {
//			
//		}
		return op;
	}
	
	private OrderPart createorderPart(
			SellSessionForCustomer sellSessionForCustomer_,
			long creationDate_){
		OrderPart op = new OrderPart();
		op.setId(creationDate_);
		
		OrderPartMenuItem opmi = new OrderPartMenuItem();
		opmi.setId(creationDate_);
		opmi.setMenu(sellSessionForCustomer_.getStall().getMenu());
		opmi.setMenuCount(MenuCount.ONE_MENU);
		opmi.setRemovedMenuItems(new ArrayList<MenuItem>());
		op.setOrderPartMenuItem(opmi);
		
		return op;
	}
	
}
